package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 *
 * @author ttao
 * @since 2023/4/25 8:42
 */

@Api(tags = "套餐管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐管理分页条件查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐管理分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐查询：{}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.queryPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("添加套餐")
    @PostMapping
    public Result<String> saveWithDish(@RequestBody SetmealDTO setmealDTO) {
        log.info("添加套餐：{}", setmealDTO);
        return setmealService.saveWithDish(setmealDTO) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 套餐批量删除
     *
     * @return
     */
    @ApiOperation("套餐批量删除")
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam("ids") List<Long> ids) {
        log.info("删除套餐：{}", ids);
        return setmealService.deleteByIds(ids) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 根据id查询套餐数据
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("{id}")
    public Result<Object> selectById(@PathVariable Long id) {
        log.info("查询套餐：{}", id);
        SetmealVO setmealVO = setmealService.selectByIdWithDish(id);
        return setmealVO != null ? Result.success(setmealVO) : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 修改套餐
     *
     * @return
     */
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> updateById(@RequestBody SetmealVO setmealVO) {
        log.info("更新套餐：{}", setmealVO);
        return setmealService.updateById(setmealVO) > 0 ? Result.success() : Result.error(MessageConstant.SETMEAL_UPDATE_FAILED);
    }

    /**
     * 启用、禁用菜品
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("启用、禁用菜品")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("更新套餐启用/禁用状态:{}-{}", status, id);
        return setmealService.startOrStop(status, id) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }
}

